
<!-- 业务测算表  -->
<template>
<div class="profitcalculation">


  <div class="head">
    <h2>业务利润核算表</h2>
    <div class="head-ctrl">
      <div class="order-number">
        {{businessformNO}}
      </div>
      <!-- <el-button type="primary">导出</el-button> -->
    </div>

    <div class="head-number">
      编号NO：
      <el-input  :readonly="!editable"  v-model="data.bpc_no"></el-input>
    </div>


  </div>
  <div class="tables">
    <div class="order-table">
      <p class="table-title">订单情况</p>

      <el-table :data="orderTableData" @cell-click="clickEditOrderTableCell" border style="width: 100%" :span-method="spanMethod">
        <el-table-column type="index" label="序号" :index="indexMethod"></el-table-column>
        <el-table-column prop="bpc_table1_product" label="产品" min-width="100">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
              <el-tooltip effect="dark"  :content="scope.row.bpc_table1_product || '暂无内容'"  placement="top">
                <el-input  :readonly="!editable"  v-model="scope.row.bpc_table1_product" ></el-input>
              </el-tooltip>
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="bpc_table1_hscode" label="海关编码" min-width="100">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
              <el-tooltip effect="dark"  :content="scope.row.bpc_table1_hscode || '暂无内容'"  placement="top">
                <el-input  :readonly="!editable"  v-model="scope.row.bpc_table1_hscode" ></el-input>
              </el-tooltip>
            </span>
            </template>
        </el-table-column>

        <el-table-column prop="bpc_table1_quantity" label="数量" min-width="120">
          <template slot-scope="scope">
              <span  class="cell-edit-input">
                <el-input-number  :readonly="!editable"  v-model="scope.row.bpc_table1_quantity" :controls="false" @change="handleOrderTableNumberChange" :min="0"></el-input-number>
              </span>
            </template>
        </el-table-column>

        <el-table-column prop="bpc_table1_price" label="单价"  min-width="120">
          <template slot-scope="scope">
              <span  class="cell-edit-input">
                <el-input-number  :readonly="!editable"  v-model="scope.row.bpc_table1_price" :controls="false" @change="handleOrderTableNumberChange" :min="0"></el-input-number>
              </span>
            </template>
        </el-table-column>

        <el-table-column prop="bpc_table1_lineAmount" label="金额"   min-width="120">
          <template slot-scope="scope">
              <span  class="cell-edit-input">
                <el-input-number v-if="scope.row.specialColumn" :disabled="true" @change="handleOrderTableNumberChange4"  :controls="false" :min="0" v-model="scope.row.bpc_table1_lineAmount" ></el-input-number>
                <el-input-number v-else :disabled="!editable"  :controls="false" @change="handleOrderTableNumberChange2" :min="0" v-model="scope.row.bpc_table1_lineAmount" ></el-input-number>
              </span>
            </template>
        </el-table-column>

        <el-table-column prop="bpc_table1_taxRebates" label="退税率" min-width="120">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
                <el-input-number   :disabled="!editable" v-model="scope.row.bpc_table1_taxRebates" :step="0.1" :max="100" :controls="false" @change="handleOrderTableNumberChange2" :min="0"></el-input-number>
                <span>%</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="bpc_table1_lineTaxRebatesAmount" label="退税款"   min-width="120">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
              <el-input-number v-if="scope.row.specialColumn"  :disabled="!editable"  v-model="scope.row.bpc_table1_lineTaxRebatesAmount" :controls="false" :min="0" ></el-input-number>
              <el-input-number v-else :disabled="!editable" @change="handleOrderTableNumberChange3" v-model="scope.row.bpc_table1_lineTaxRebatesAmount" :controls="false" :min="0" ></el-input-number>
            </span>
          </template>
        </el-table-column>

        <el-table-column
        v-if="editable"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="addNewLine(scope.$index, data.bpc_table1)" type="text" size="small">添加</el-button>
          <el-button @click="removeCurrentLine(scope.$index, data.bpc_table1)" type="text" size="small">移除</el-button>
        </template>
      </el-table-column>
      </el-table>
    </div>

    <div class="service-table">
      <p class="table-title">服务情况</p>
      <el-table :data="serviceTableData" @cell-click="clickEditServiceTableCell" border style="width: 100%">
        <el-table-column prop="bpc_table2_foreignTradeContractAmount" label="外贸合同金额"  min-width="100">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
              <el-input-number  :disabled="!editable"  v-if="scope.row.bpc_table2_first" v-model="scope.row.bpc_table2_foreignTradeContractAmount" :controls="false" @change="handleServiceTableNumberChange" :min="0"></el-input-number>
              <div v-else class="placeholder"></div>
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="bpc_table2_securityDeposit" label="保证金"  min-width="100">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
              <el-input-number  :disabled="!editable"  v-if="scope.row.bpc_table2_first"  v-model="scope.row.bpc_table2_securityDeposit" :controls="false" :min="0"></el-input-number>
              <div v-else class="placeholder"></div>
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="bpc_table2_foreignTradeCreditAmount" label="外贸赊销款" min-width="100">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
              <el-input-number  :disabled="!editable"  v-if="scope.row.bpc_table2_first"   v-model="scope.row.bpc_table2_foreignTradeCreditAmount" :controls="false" :min="0"></el-input-number>
              <div v-else class="placeholder"></div>
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="bpc_table2_domesticTradeContractAmount" label="内贸合同金额"  min-width="130">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
              <el-input-number  :disabled="!editable"   v-model="scope.row.bpc_table2_domesticTradeContractAmount" :controls="false"  :min="0"></el-input-number>
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="bpc_table2_factoryDownPayment" label="工厂定金"  min-width="100">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
              <el-input-number  :disabled="!editable"  v-if="scope.row.bpc_table2_first"  v-model="scope.row.bpc_table2_factoryDownPayment" :controls="false"  :min="0"></el-input-number>
              <div v-else class="placeholder"></div>
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="bpc_table2_factoryRetainage" label="工厂尾款"   min-width="100">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
              <el-input-number  :disabled="!editable"  v-if="scope.row.bpc_table2_first"  v-model="scope.row.bpc_table2_factoryRetainage" :controls="false" :min="0"></el-input-number>
              <div v-else class="placeholder"></div>
            </span>
          </template>
        </el-table-column>


        <el-table-column prop="bpc_table2_creditSalesAmount" label="赊销金额" min-width="100">
          <template slot-scope="scope">
            <span  class="cell-edit-input">
              <el-input-number  :disabled="!editable"  v-if="scope.row.bpc_table2_first"  v-model="scope.row.bpc_table2_creditSalesAmount" :controls="false" :min="0"></el-input-number>
              <div v-else class="placeholder"></div>
            </span>
          </template>
        </el-table-column>


        <el-table-column label="服务费" >

          <el-table-column prop="bpc_table2_agentRefund" label="代理退税费">
            <template slot-scope="scope">
              <span  class="cell-edit-input">

                <template v-if="scope.row.bpc_table2_agentRefund !== undefined">

                  <el-input-number class="spec-rate" v-if="scope.row.isRate"  :disabled="!editable"   v-model="scope.row.bpc_table2_agentRefund"
                  :controls="false" @change="handleServiceTableNumberChange" :step="0.1" :min="0" :max="100" ></el-input-number>
                  <span v-if="scope.row.isRate">%</span>

                  <el-input-number  v-else  :disabled="true"   v-model="scope.row.bpc_table2_agentRefund"
                  :controls="false" @change="handleServiceTableNumberChange" :min="0" ></el-input-number>
                </template>
                <div v-else class="placeholder"></div>

              </span>
            </template>
          </el-table-column>

          <el-table-column prop="bpc_table2_creditExportServices" label="赊销出口服务费">
            <template slot-scope="scope">
              <span  class="cell-edit-input">
                <template v-if="scope.row.bpc_table2_creditExportServices !== undefined">
                  <el-input-number class="spec-rate"  v-if="scope.row.isRate"  :disabled="!editable"   v-model="scope.row.bpc_table2_creditExportServices"
                  :controls="false" @change="handleServiceTableNumberChange" :step="0.1" :min="0" :max="100" ></el-input-number>
                  <span v-if="scope.row.isRate">%</span>

                  <el-input-number  v-else  :disabled="true"   v-model="scope.row.bpc_table2_creditExportServices"
                  :controls="false" @change="handleServiceTableNumberChange" :min="0" ></el-input-number>
                </template>
                <div v-else  class="placeholder"></div>
              </span>
            </template>
          </el-table-column>

          <el-table-column prop="bpc_table2_financingService" label="融资服务费">
            <template slot-scope="scope">
              <span  class="cell-edit-input">

                <template v-if="scope.row.bpc_table2_financingService !== undefined">
                  <el-input-number class="spec-rate" v-if="scope.row.isRate"  :disabled="!editable"   v-model="scope.row.bpc_table2_financingService"
                  :controls="false" @change="handleServiceTableNumberChange" :step="0.1" :min="0" :max="100" ></el-input-number>
                   <span v-if="scope.row.isRate">%</span>
                  <el-input-number  v-else  :disabled="true"   v-model="scope.row.bpc_table2_financingService"
                  :controls="false" @change="handleServiceTableNumberChange" :min="0" ></el-input-number>
                </template>
                <div v-else  class="placeholder"></div>
              </span>
            </template>
          </el-table-column>
          </el-table-column>

      </el-table>
    </div>

    <div class="other">
      <p class="table-title">其他收费说明</p>
      <pre>
      <p>汇   率: <el-input  :readonly="!editable"  v-model="data.bpc_exchangeRate"></el-input></p>
      <p>物   流: <el-input  :readonly="!editable"  v-model="data.bpc_logistics"></el-input></p>
      <p>中介费用: <el-input  :readonly="!editable"  v-model="data.bpc_agencyFee"></el-input></p>
      <p>其  他 : <el-input  :readonly="!editable"  v-model="data.bpc_other"></el-input></p>
    </pre>

    </div>



  <div class="upload">
    <p class="upload-hint">票据/附件</p>
    <div>
      <div class="upload-btn">
        <input  type="file"  v-show="editable"  name="file" multiple @change="selectFiles" >
        <el-button :disabled="!editable" type="primary">选择附件</el-button>
      </div>


      <ul class="upload-files">
        <li @mouseover="handleOver(item)" :key="index"   @mouseout="handleOut(item)" v-for="(item,index) in data.attachedFiles">
          <span @click="download(item.id, $event)">{{item.name}}</span>
          <span v-if="editable && item.status" class="el-icon-circle-check" style="color:#67c23a;"></span>
          <span v-else-if="editable" class="el-icon-circle-close" @click="changeFiles(index,item)" style="color:red;"></span>
        </li>
      </ul>
    </div>
  </div>

  </div>



</div>
</template>

<script>
import {uploadFiles} from '@/server/initiatingprocess';
import {filedownload , deleteFile} from '@/server/customermanagement/index.js';
import {toFixed} from '@/util/transform.js';

export default {
  methods: {
    indexMethod(index) {
      let lines = this.data.bpc_table1.length;
      if (lines - 1 === index) {
        return '合计Total';
      }
      return index + 1;
    },

    selectFiles(event){
      //票据附件
      let _this = this;


      //直接上传，并且获得后端返回的文件的url,将url保存到 this.data.attachedFiles中
      uploadFiles(event.target.files).then((resArr)=>{
        resArr.forEach(function(res,i){
          _this.data.attachedFiles.push({name: event.target.files[i].name, status: 1, id: res.data.datas[0].id});
        });

        _this.$forceUpdate();
      });
    },
    changeFiles(index,item) {
      deleteFile({'id':item.id}).then((res) => {
        if(res.code != 1 ){
          this.$message.error('删除失败，请检查网络后重试');
        } else {
          this.data.attachedFiles.splice(index, 1);
        }
      });
    },
    handleOver(item){
      item.status = 0;
      this.$forceUpdate();
    },
    handleOut(item){
      item.status = 1;
      this.$forceUpdate();
    },
    download(id, event){
      window.open('http://47.104.93.41/api/common/download?id='+id);
    },

    clickEditOrderTableCell(row, column, cell, event) {
      //点击修改单元格
      // console.log(row);

      if (row) {
        this.clickTableIndex = row.rowIndex;
      }


      if (this.tableChanged) {
        this.updateOrderTableData();
        this.$forceUpdate();
        // this.tableChanged = false;
        return;
      }

      if (this.tableChanged2) {
        this.updateAmount();
        this.$forceUpdate();
        this.tableChanged2 = false;
        return;
      }

      if (this.tableChanged3) {
        this.updateTaxAmount();
        this.$forceUpdate();
        this.tableChanged3 = false;
        return;
      }
    },

    handleOrderTableNumberChange(value) {

      if (!this.specialClick) {
        this.tableChanged = true;
        this.clickEditOrderTableCell();
      } else {
        this.tableChanged = false;
        this.specialClick = false;
      }
    },

    handleOrderTableNumberChange2(value){

      this.isUpdateAmount = true;
      this.tableChanged2 = true;
      this.clickEditOrderTableCell();


    },

    handleOrderTableNumberChange3(value){
      this.isUpdateTaxAmount = true;
      this.tableChanged3 = true;
      this.clickEditOrderTableCell();
    },

    handleOrderTableNumberChange4(){
      this.updateTotal();
      this.$forceUpdate();
    },

    updateTotal(){
      let current = this.data.bpc_table1, lines = current.length;
      this.data.bpc_table2[0].bpc_table2_foreignTradeContractAmount = toFixed(current[lines - 1].bpc_table1_lineAmount, 2);
    },

    updateTaxAmount(){
      let current = this.data.bpc_table1,
      amounts = 0,
      taxAmounts = 0;

      let lines = current.length;

      for (let i = 0; i < lines - 1; i++) {
        current[i].bpc_table1_lineTaxRebatesAmount = toFixed(current[i].bpc_table1_lineTaxRebatesAmount, 2);
        taxAmounts += parseFloat(current[i].bpc_table1_lineTaxRebatesAmount);
      }

      current[lines - 1].bpc_table1_lineTaxRebatesAmount = toFixed(taxAmounts,2);
    },


    updateAmount(){
      let current = this.data.bpc_table1,
      amounts = 0,
      taxAmounts = 0;

      let lines = current.length, currLine = this.clickTableIndex ;


      current[currLine].bpc_table1_lineAmount = toFixed(current[currLine].bpc_table1_lineAmount, 2);
      current[currLine].bpc_table1_taxRebates = toFixed(current[currLine].bpc_table1_taxRebates, 2);

      current[currLine].bpc_table1_lineTaxRebatesAmount = toFixed((current[currLine].bpc_table1_lineAmount / 1.17 * current[currLine].bpc_table1_taxRebates/100), 2);

      for (let i = 0; i < lines - 1; i++) {
        amounts += parseFloat(current[i].bpc_table1_lineAmount);
        taxAmounts += parseFloat(current[i].bpc_table1_lineTaxRebatesAmount);
      }

      this.data.bpc_table2[0].bpc_table2_foreignTradeContractAmount = toFixed(amounts,2);

      current[lines - 1].bpc_table1_lineAmount = toFixed(amounts,2);
      current[lines - 1].bpc_table1_lineTaxRebatesAmount = toFixed(taxAmounts,2);
    },

    updateOrderTableData(){
      let current = this.data.bpc_table1,
        amounts = 0,
        taxAmounts = 0;

      let lines = current.length, correctValue = 0, correctTax = 0;

      for (let i = 0; i < lines - 1; i++) {

        current[i].bpc_table1_quantity  = parseInt(current[i].bpc_table1_quantity);
        current[i].bpc_table1_price = toFixed(current[i].bpc_table1_price, 2);
        current[i].bpc_table1_taxRebates = toFixed(current[i].bpc_table1_taxRebates, 2);

        correctValue = toFixed((current[i].bpc_table1_quantity * current[i].bpc_table1_price), 2);
        correctTax = toFixed((correctValue / 1.17 * current[i].bpc_table1_taxRebates/100), 2);

        if (current[i].bpc_table1_lineAmount && (current[i].bpc_table1_lineAmount != correctValue)){
        } else {
          current[i].bpc_table1_lineAmount = toFixed(correctValue, 2);
        }

        if (current[i].bpc_table1_lineTaxRebatesAmount && (current[i].bpc_table1_lineTaxRebatesAmount != correctTax)){
        } else {
         current[i].bpc_table1_lineTaxRebatesAmount = toFixed(correctTax, 2);
        }

        if (i == this.clickTableIndex ) {
          current[i].bpc_table1_lineAmount = toFixed(correctValue, 2);
          current[i].bpc_table1_lineTaxRebatesAmount = toFixed(correctTax, 2);
        }

         current[i].bpc_table1_lineAmount = toFixed(current[i].bpc_table1_lineAmount, 2);
         current[i].bpc_table1_lineTaxRebatesAmount = toFixed(current[i].bpc_table1_lineTaxRebatesAmount, 2);

        current[i].rowIndex = i;
        current[i].specialColumn = false;

        amounts += parseFloat(current[i].bpc_table1_lineAmount);
        taxAmounts += parseFloat(current[i].bpc_table1_lineTaxRebatesAmount);
      }


      this.data.bpc_table2[0].bpc_table2_foreignTradeContractAmount = toFixed(amounts, 2);

      current[lines - 1].bpc_table1_lineAmount = toFixed(amounts, 2);
      current[lines - 1].bpc_table1_lineTaxRebatesAmount = toFixed(taxAmounts,2);
    },

    clickEditServiceTableCell() {
    },

    handleServiceTableNumberChange() {
      this.updateServiceTableData();
      this.$forceUpdate();
    },

    updateServiceTableData(){
      let rates = [], amount = 0;

      let current = this.data.bpc_table2;
      current.forEach((e, i, a) => {
        if (0 === i) {

          e.bpc_table2_agentRefund = toFixed(e.bpc_table2_agentRefund, 2);
          e.bpc_table2_creditExportServices = toFixed(e.bpc_table2_creditExportServices, 2);
          e.bpc_table2_financingService = toFixed(e.bpc_table2_financingService, 2);

          rates[0] = e.bpc_table2_agentRefund / 100;
          rates[1] = e.bpc_table2_creditExportServices / 100;
          rates[2] = e.bpc_table2_financingService / 100;
          amount = e.bpc_table2_foreignTradeContractAmount;
        } else if (1 === i) {
          e.bpc_table2_agentRefund = toFixed((rates[0] * amount), 2);
          e.bpc_table2_creditExportServices = toFixed((rates[1] * amount), 2);
          e.bpc_table2_financingService = toFixed((rates[2] * amount), 2);
        }
      });
    },

    addNewLine(currentIndex, table){

      this.specialClick = true;
      let newLine = _.cloneDeep(table[currentIndex]);

      // for (let prop in newLine) {
      //   newLine[prop] = '';
      // }

      table.splice(currentIndex + 1, 0, newLine);

      table.forEach((e,i) => {
        e.rowIndex =  i;
      });

      this.handleOrderTableNumberChange();
    },
    removeCurrentLine(currentIndex, table){
      this.specialClick = true;

      if (table.length > 2){
        table.splice(currentIndex, 1);
        table.forEach((e,i) => {
          e.rowIndex =  i;
        });
      }
      this.handleOrderTableNumberChange();
    },
    spanMethod({
      row,
      column,
      rowIndex,
      columnIndex
    }) {
      let lines = this.data.bpc_table1.length;
      if (lines - 1 === rowIndex) {
        switch (columnIndex) {
          case 0:
            return [1, 5];
            break;
          case 1:
          case 2:
          case 3:
          case 4:
          case 6:
          case 8:
          return [0, 0];
          case 5:
          case 7:
          return [1, 2];
        }
      }
    },
        /**选择币种 */
    currencySel(name){



       return (h, {column, $index}) => {
        let self = this;

       var selAttr = {};
       if (!this.editable){
         selAttr.disabled = 'disabled';
       }
        return  h("div", {
          attrs:  {
            class: "select-currency"
          }
        }, [
          h("span", name),
          h("select", {
            attrs: {...selAttr},
            domProps: {
            value: self.currencyValue
          },
          on: {
            change: function(event){
              self.currencyValue = event.target.value;
              self.data.bpc_currencyValue = event.target.value;
            }
          }
          },[
            h("option", "人民币"),
            h("option", "美元"),
            h("option", "加元"),
            h("option", "欧元"),
          ])
        ]);
      }
    }
  },
  created(){

  },
  mounted(){
  },

  updated(){


    if (this.isUpdateAmount){
      this.isUpdateAmount = false;
      this.updateAmount();
    } else if(this.isUpdateTaxAmount) {
      this.isUpdateTaxAmount = false;
      this.updateTaxAmount();
    } else if (this.tableChanged) {
      this.updateOrderTableData();
      this.tableChanged = false;
    }

    this.updateTotal();
    this.updateServiceTableData();

    if (this.clickTab  && this.relation) {



      //第一次使用 关联的值
      if(!this.fromOther){

        this.data.bpc_currencyValue = this.relatedData2.bf_currencyValue;
        this.currencyValue = this.relatedData2.bf_currencyValue;
        
        //表格1 关联 businessform_pi  relatedData1
        if (this.relatedData1.hasClicked) {
          let currRelated1 = _.cloneDeep(this.relatedData1.bp_table1);

          if (!_.isEqual(currRelated1, this.oldRelated1)){

            this.data.bpc_table1.splice(this.relatedData1.bp_table1.length);
            for (let i = 0; i < this.relatedData1.bp_table1.length; i++) {
              if (!this.data.bpc_table1[i]){
                this.data.bpc_table1.push({
                  bpc_table1_product: '',
                  bpc_table1_hscode: '',
                  bpc_table1_quantity: '',
                  bpc_table1_price: '',
                  bpc_table1_lineAmount: '',
                  bpc_table1_taxRebates: '',
                  bpc_table1_lineTaxRebatesAmount: '',
                  bpc_table1_amounts: '',
                  bpc_table1_taxRebatesAmounts: ''
                });
              }
              (this.data.bpc_table1[i]).bpc_table1_product =  (this.relatedData1.bp_table1[i]).bp_table1_products;
              (this.data.bpc_table1[i]).bpc_table1_hscode = (this.relatedData1.bp_table1[i]).bp_table1_hscode;
              (this.data.bpc_table1[i]).bpc_table1_quantity = parseInt((this.relatedData1.bp_table1[i]).bp_table1_quantity);
              (this.data.bpc_table1[i]).bpc_table1_price = toFixed((this.relatedData1.bp_table1[i]).bp_table1_price, 2);

              (this.data.bpc_table1[i]).bpc_table1_lineAmount = toFixed((this.relatedData1.bp_table1[i]).bp_table1_lineAmount, 2);
              (this.data.bpc_table1[i]).rowIndex = i;

              (this.data.bpc_table1[i]).bpc_table1_taxRebates = toFixed((this.data.bpc_table1[i]).bpc_table1_taxRebates, 2) || 0;
              (this.data.bpc_table1[i]).bpc_table1_lineTaxRebatesAmount = toFixed((this.data.bpc_table1[i]).bpc_table1_lineTaxRebatesAmount,2)  || toFixed(((this.data.bpc_table1[i]).bpc_table1_lineAmount / 1.17 * (this.data.bpc_table1[i]).bpc_table1_taxRebates/100),2);
            }
            

            this.data.bpc_table1[this.relatedData1.bp_table1.length - 1].specialColumn = true;

            this.relatedData1.relatedChangedStatusPR = 2;
            this.oldRelated1 = _.cloneDeep(currRelated1);
          }

        } else {
          this.oldRelated1 = _.cloneDeep(this.relatedData1.bp_table1);
        }


        if (this.relatedData2.hasClicked) {

          this.data.bpc_no =  this.relatedData2.bf_orderNumber;

          let currRelated2 = _.cloneDeep((this.relatedData2.bf_table1[this.relatedData2.bf_table1.length - 2].bf_table1_lineAmount).toString().slice(3));

          if(!_.isEqual(currRelated2, this.oldRelated2)){
            //表格2 关联  businessform_foreigntradecontract    relatedData2 的金额
            this.data.bpc_table2[0].bpc_table2_foreignTradeContractAmount = parseFloat((this.relatedData2.bf_table1[this.relatedData2.bf_table1.length - 2].bf_table1_lineAmount).toString().slice(3).replace(/,/g, ''));
            

            this.relatedData2.relatedChangedStatusPR = 2;
            this.oldRelated2 = _.cloneDeep(currRelated2);
          }

        } else {
          this.oldRelated2 = _.cloneDeep((this.relatedData2.bf_table1[this.relatedData2.bf_table1.length - 2].bf_table1_lineAmount).toString().slice(3));
        }



        let clickDO = false;
        clickDO = this.relatedData3.some(e => {
          return e.hasClicked == true;
        });

        if (clickDO){
          let currRelated3 = _.cloneDeep(this.relatedData3);

          if (!_.isEqual(currRelated3, this.oldRelated3)){

            this.data.bpc_table2.splice(3);

            //表格2 关联  businessform_domestictradecontract relatedData3 的金额
            for (let i = 0; i < this.relatedData3.length; i++) {
              if (!this.data.bpc_table2[i]) {
                this.data.bpc_table2.push({});
              }
              let temp = this.relatedData3[i].bd_table1;
              this.data.bpc_table2[i].bpc_table2_domesticTradeContractAmount =  temp[temp.length - 1].bd_table1_lineAmount;

              this.relatedData3[i].relatedChangedStatusPR = 2;
            }

            this.oldRelated3 = _.cloneDeep(currRelated3);
          }

        } else {
          this.oldRelated3 = _.cloneDeep(this.relatedData3);
        }
      } else {
        this.currencyValue = this.data.bpc_currencyValue;
      }

      // this.fromOther = false;

      this.clickTab = false;
      this.$forceUpdate();
    } else {
      this.oldRelated1 = _.cloneDeep(this.relatedData1.bp_table1);
      this.oldRelated2 = _.cloneDeep((this.relatedData2.bf_table1[this.relatedData2.bf_table1.length - 2].bf_table1_lineAmount).toString().slice(3));
      this.oldRelated3 = _.cloneDeep(this.relatedData3);
    }

  },
  computed: {
    orderTableData() {
      let current = this.data.bpc_table1, amounts = 0, taxAmounts = 0;
      let lines = current.length, correctValue = 0, correctTax = 0;

      for (let i = 0; i < lines - 1; i++) {
        current[i].bpc_table1_quantity = parseInt(current[i].bpc_table1_quantity); 
        current[i].bpc_table1_price = toFixed(current[i].bpc_table1_price, 2);
        current[i].bpc_table1_lineAmount = toFixed(current[i].bpc_table1_lineAmount, 2);
        current[i].bpc_table1_taxRebates = toFixed(current[i].bpc_table1_taxRebates, 2);

        correctValue = toFixed((current[i].bpc_table1_quantity * current[i].bpc_table1_price), 2);
        correctTax = toFixed((current[i].bpc_table1_lineAmount / 1.17 * current[i].bpc_table1_taxRebates/100), 2);

        if (current[i].bpc_table1_lineAmount && (current[i].bpc_table1_lineAmount != correctValue)){
        } else {
          current[i].bpc_table1_lineAmount = toFixed(correctValue, 2);
        }

        if (current[i].bpc_table1_lineTaxRebatesAmount && (current[i].bpc_table1_lineTaxRebatesAmount != correctTax)){
        } else {
          current[i].bpc_table1_lineTaxRebatesAmount = correctTax;
        }

        current[i].bpc_table1_lineAmount = toFixed(current[i].bpc_table1_lineAmount, 2);
        current[i].bpc_table1_lineTaxRebatesAmount = toFixed((current[i].bpc_table1_lineAmount / 1.17 * current[i].bpc_table1_taxRebates/100), 2);
        current[i].rowIndex = i;
      }

      for (let i = 0; i < lines - 1; i++) {
        amounts += parseFloat(current[i].bpc_table1_lineAmount);
        taxAmounts += parseFloat(current[i].bpc_table1_lineTaxRebatesAmount);
      }


      current[lines - 1].bpc_table1_lineAmount = toFixed(amounts, 2);
      current[lines - 1].bpc_table1_lineTaxRebatesAmount = toFixed(taxAmounts, 2);
      current[lines - 1].specialColumn = true;

      return current;
    },

    serviceTableData() {

      let rates = [],
        amount = 0;

        let current = this.data.bpc_table2;
        current.forEach((e, i, a) => {
          if (0 === i) {

            e.bpc_table2_agentRefund = toFixed(e.bpc_table2_agentRefund, 2);
            e.bpc_table2_creditExportServices = toFixed(e.bpc_table2_creditExportServices, 2);
            e.bpc_table2_financingService = toFixed(e.bpc_table2_financingService, 2);

            rates[0] = e.bpc_table2_agentRefund / 100;
            rates[1] = e.bpc_table2_creditExportServices / 100;
            rates[2] = e.bpc_table2_financingService / 100;
            amount = e.bpc_table2_foreignTradeContractAmount;
            e.isRate = true;
          } else if (1 === i) {
            e.bpc_table2_agentRefund = toFixed((rates[0] * amount), 2);
            e.bpc_table2_creditExportServices = toFixed((rates[1] * amount), 2);
            e.bpc_table2_financingService = toFixed((rates[2] * amount), 2);
          }
        });
      return current;
    }
  },
  props: {
    businessformNO: {
      type: String,
      required: true
    },
    data: {
      type: Object,
      required: true
    },
    relatedData1: {
      type: Object,
      required: true
    },
    relatedData2: {
      type: Object,
      required: true
    },
    relatedData3: {
      type: Array,
      required: true
    },
    editable: {
      type: Boolean,
      required: true
    },
    relation: {
      type: Boolean,
      required: true
    },
    fromOther: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      isUpdateAmount: false,
      isUpdateTaxAmount: false,
      tableChanged: false,
      tableChanged2: false,
      tableChanged3: false,
      specialClick: false,
      clickTableIndex: 0,
      clickTab: false,
      oldRelated1: [],
      oldRelated2: 0,
      oldRelated3: [],
      currencyValue: '人民币'
    }
  }
}
</script>

<style lang="scss">


.profitcalculation {
    padding: 20px;
    border: 1px solid #999;

    .select-currency {
      select {
        margin-left: 12px;
        height: 30px;
        width: 60px;
      }
    }
    .head {
      > h2 {
        text-align: center;
      }
      .head-ctrl {
        height: 60px;
        .order-number {
          padding: 16px 0px;
          float: left;
        }
        button {
          float: right;
        }
      }

      .head-number {
        .el-input {
          width: 400px;
        }
      }
    }

    .tables {
        .cell {
            padding: 0;
        }

        th {
            background: #e6e6e6 !important;
        }

        .table-title {
            padding: 15px 0;
        }
        .el-table--border td,
        .el-table--border th {
            border: 1px solid #e6ebf5;
        }

        .cell-edit-input {
            .el-input__inner {
                border: none;
                background: transparent;
                padding: 0;
                text-align: center;
            }

            .el-input-number__decrease,
            .el-input-number__increase {
                width: 20px;
                border: none;
                background: transparent;
            }

            .el-input-number {
                width: 100px;
                min-width: 100px;
            }
        }

        .service-table {
            .el-input-number {
                width: 80px;
                min-width: 80px;
            }

            .spec-rate {
              width: 60px;
              min-width: 60px;
            }
        }

        .other {
            .el-input {
                width: 500px;
            }
        }
    }

    .upload {
      >div {
        border: 1px solid #999;
        padding: 20px 0px 40px 20px;
      }

      .upload-btn {
        position: relative;
        >input {
          opacity: 0;
          width: 100px;
          height: 40px;
          position: absolute;
          top: 0;
          left: 0;
        }
      }

      .upload-files {

          > li {
              width: 300px;
              padding: 10px 0;
              transition: all 0.5s linear;

              &:hover {

                >span {
                  color: #409EFF;
                }
              }
            }

          font-size: 16px;
          span {
              padding: 0 10px;
              cursor: pointer;
              display: inline;
          }
          span.el-icon-close:hover {
              cursor: pointer;
          }
      }
      .upload-hint {
        font-size: 20px;
        margin-bottom: 10px;
      }
    }
}
</style>
